package com.tensquare.article.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.article.pojo.Column;
import com.tensquare.article.service.ColumnService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import util.IdWorker;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/column")
@CrossOrigin
public class ColumnController {

    @Autowired
    private ColumnService columnService;


    //POST/column增加专栏
    @PostMapping
     public Result save(@RequestBody Column column){
        columnService.save(column);
        return new Result(true, StatusCode.OK,"ok");
    }
  //  GET/column 专栏全部列表
    @GetMapping
    public Result findAll() {
     List<Column> list =columnService.findAll();
        return  new Result(true,StatusCode.OK,"ok",list);
  }
   // GET/column/{columnId}根据ID查询专栏
    @GetMapping("{columnId}")
     public Result findById(@PathVariable String columnId) {
        Column column=columnService.findById(columnId);
        return new Result(true,StatusCode.OK,"ok",column);
    }
    //PUT/column/{columnId} 修改Column
    @PutMapping("{columnId}")
    public Result findById(@PathVariable String columnId,@RequestBody Column column) {
        column.setId(columnId);
        columnService.updateById(column);
        return new Result(true,StatusCode.OK,"ok");
    }
   // DELETE/column/{columnId} 根据ID删除Column
   @DeleteMapping("{columnId}")
   public Result deleteById(@PathVariable String columnId) {
        columnService.deleteById(columnId);
        return new Result(true,StatusCode.OK,"ok");
   }
    //POST/column/search/{page}/{size} 专栏分页
    @PostMapping("/search/{page}/{size}")
    public Result findByPage(@PathVariable Integer page,
                             @PathVariable Integer size,
                             @RequestBody Map<String,Object> map) {
        Page<Column> pageData =columnService.findByPage(page,size,map);
        PageResult<Column> pageResult=new PageResult<>(pageData.getTotal(),pageData.getRecords());
        return new Result(true,StatusCode.OK,"ok",pageResult);
    }
    //POST/column/searchs 条件查询
    @PostMapping("/search")
    public Result findByOne( @RequestBody Map<String,Object> map) {
       List<Column> list=columnService.findByOne( map);
       return  new Result(true,StatusCode.OK,"ok",list);
    }
    }
